package regexp;

import regexp.utils.RegExpFind;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author DCSGO
 * @version 1.0
 * 分组
 */
public class Regexp07 {
    public static void main(String[] args) {
        //在正则表达式中的出现的一对括号为一个分组 分组序号1开始 从左至右递增
        //(pattern) 非命名分组
        RegExpFind.printFindGroups("what123456 do1234 you112233 do1214", "(\\d\\d)(\\d\\d)", 2);
        /*
         * what123456 do1234 you112233 do1214 通过 (\d\d)(\d\d)匹配到：
         * 找到 1
         * 1234 12 34
         * 找到 2
         * 1234 12 34
         * 找到 3
         * 1122 11 22
         * 找到 4
         * 1214 12 14
         */

        System.out.println("-----------------------------------------");
        //(?<name>pattern) 命名分组 在之后可以使用分组名来得到匹配到的分组
        Pattern pattern = Pattern.compile("(?<g1>\\d\\d)(?<g2>\\d\\d)");
        Matcher matcher = pattern.matcher("what123456 do1234 you112233 do1214");
        while (matcher.find()) {
            System.out.println("找到 " + matcher.group(0) + " g1: "
                    + matcher.group("g1") + " g2: " + matcher.group("g2"));
        }
        /*
         * 找到 1234 g1: 12 g2: 34
         * 找到 1234 g1: 12 g2: 34
         * 找到 1122 g1: 11 g2: 22
         * 找到 1214 g1: 12 g2: 14
         */
    }
}
